import java.util.ArrayList;
import java.util.List;

public class PaperList {
    private final String teacher;
    private final List<Paper> papers;
    private final PaperSelection selections = new PaperSelection();
    private final PaperSelectionStrategy strategy;

    public PaperList(String teacher, List<Paper> papers, PaperSelectionStrategy strategy) {
        this.teacher = teacher;
        this.papers = papers;
        this.strategy = strategy;
    }

    private List<Paper> getUnselectedPapersByType(PaperType type) {
        List<Paper> result = new ArrayList<>();
        for (Paper paper : papers) {
            if (!selections.isSelected(paper) && 
                (type == PaperType.Any || 
                 (type == PaperType.Thesis && paper instanceof Thesis) ||
                 (type == PaperType.JournalPaper && paper instanceof JournalPaper) ||
                 (type == PaperType.ConferencePaper && paper instanceof ConferencePaper))) {
                result.add(paper);
            }
        }
        return result;
    }

    public List<Paper> selectPapers(Student student) throws NoEnoughPapersException {
        List<Paper> availablePapers = getUnselectedPapersByType(student.getPreferredPaperType());
        if (availablePapers.size() < student.getPreferredQuantity()) {
            throw new NoEnoughPapersException(
                student.getName(), 
                student.getPreferredQuantity(), 
                availablePapers.size()
            );
        }
        List<Paper> selected = strategy.selectPapers(availablePapers, student.getPreferredQuantity());
        for (Paper paper : selected) {
            selections.addSelection(student, paper);
        }
        return selected;
    }

    public PaperSelection getSelectionResult() {
        return selections;
    }
}    